package com.scalar.chiptrack.commons.actions;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.alarms.Alarm;
import com.scalar.chiptrack.alarms.AlarmUser;
import com.scalar.chiptrack.alarms.dao.AlarmDAO;
import com.scalar.chiptrack.sales.dao.quote.QuotationDAO;
import com.scalar.chiptrack.sales.dao.NewCustomerDAO;
import com.scalar.chiptrack.sales.SalesPerson;
import com.scalar.chiptrack.utils.ConfigManager;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.commons.InternalComment;
import com.scalar.chiptrack.commons.dao.InternalCommentsDAO;
import com.scalar.chiptrack.commons.forms.CreatePartNumberForm;
import com.scalar.chiptrack.users.User;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.util.ResponseUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: munugoti
 * Date: 17 Nov, 2010
 * Time: 10:44:30 PM
 * To change this template use File | Settings | File Templates.
 */
public class CreatePartNumberAction extends ChipTrackAction
{
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        HttpSession session = request.getSession();
        User user = ( User ) session.getAttribute( SessionKeys.USER_KEY );
        if( user == null )
        {
            // Write XML to response.
            response.setContentType("application/xml");
            String responseStr = "<commts><commt status=' '>Session has been time out, Your request could not be processed. Please login again</commt></commts>";
            response.getWriter().write( responseStr );
            return null;
        }
        CreatePartNumberForm createPartNumberForm = (CreatePartNumberForm)form;
        String action = request.getParameter( "action" );

        if ("createPartNumber".equalsIgnoreCase( action ) )
        {
            String status = InternalCommentsDAO.insertFgPartNumber( null, createPartNumberForm );
            String responseStr = getResponseStatusXML(status);
            response.setContentType("application/xml");
            response.getWriter().write( responseStr );
        }
        return null;
    }
    private String getResponseStatusXML ( String status )
    {
        StringBuffer commentsXML = new StringBuffer();
        commentsXML.append ( "<commts>" );
        commentsXML.append ( "<commt status='" ).append ( status ).append ( "'>");
        commentsXML.append ( "</commt>" );
        commentsXML.append ( "</commts>" );
        return commentsXML.toString();
    }
}
